'use strict'

let config = require('../../config')

let um = require('unique-model');
let Types = um.Types;
let Text = Types.Text;
let Integer = Types.Integer;
let Double = Types.Double;
let Bool = Types.Bool;
let DateTime = Types.DateTime;
let UObject = Types.UObject;
let UObjectArray = Types.UObjectArray;

um.enablePersist();
let session = um.createSession(config.database);

let User = um.model.createModel('User', {
    iflyId: Text(),
    name: Text(),
    password: Text(),
    realName: Text(),
    role: UObject({
        type: 'Role'
    }),
    status: Integer()
});

let Role = um.model.createModel('Role', {
    name: Text(),
    privileges: UObjectArray({
        type: 'Privilege'
    })
});

let Privilege = um.model.createModel('Privilege', {
    name: Text()
});

let School = um.model.createModel('School', {
    iflyId: Text(),
    name: Text(),
    grades: UObjectArray({
        type: 'Grade'
    }),
    classrooms: UObjectArray({
        type: 'Classroom'
    }),
    festivals: UObjectArray({
        type: 'Festival'
    }),
    //checkTimeSet: UObject({
    //    type: 'CheckTimeSet'
    //}),
    terms: UObjectArray({
        type: 'Term'
    }),
    devices: UObjectArray({
        type: 'Device'
    }),
    labels:UObjectArray({
        type: 'Label'
    }),
    attendanceRules:UObjectArray({
        type:'AttendanceRule'
    })
});

let Grade = um.model.createModel('Grade', {
    name: Text(),
    iflySchoolId: Text(),
    natureClasses: UObjectArray({
        type: 'NatureClass'
    })
    //checkTimeSet: UObject({
    //    type: 'CheckTimeSet'
    //})
});

let NatureClass = um.model.createModel('NatureClass', {
    iflyId: Text(),
    name: Text(),
    students: UObjectArray({
        type: 'Student'
    })
});

let Student = um.model.createModel('Student', {
    name: Text(),
    sid: Text(),
    natureClass: UObject({
        type: 'NatureClass'
    }),
    cards: UObjectArray({
        type: 'Card'
    }),
    user: UObject({
        type: 'User'
    }),
    labels:UObject({
        type: 'Label'
    }),
    atSchool: Bool(),
    attendCount: Integer(),
    totalAbsence: Integer(),
    lateCount: Integer(),
    absenceCount: Integer(),
    restCount: Integer(),
    earlyCount: Integer(),
    attendanceRule:UObject({
        type:'AttendanceRule'
    })
});

let Term = um.model.createModel('Term', {
    //name: Text(),
    startTime: DateTime(),
    endTime: DateTime()
});

let Classroom = um.model.createModel('Classroom', {
    name: Text()
});

let CheckTimeSet = um.model.createModel('CheckTimeSet', {
    weekdays: UObjectArray({
        type: 'CheckTime'
    }),
    saturday: UObjectArray({
        type: 'CheckTime'
    }),
    sunday: UObjectArray({
        type: 'CheckTime'
    })
});

let CheckTime = um.model.createModel('CheckTime', {
    startHour: Integer(),
    startMinute: Integer(),
    endHour: Integer(),
    endMinute: Integer(),
    status: Integer(),
    applicableDate:Text()
});

//let Festival = um.model.createModel('Festival', {
//    name: Text(),
//    start: DateTime(),
//    end: DateTime(),
//    status: Integer()
//});

let AttendanceRule = um.model.createModel('AttendanceRole',{
    name:Text(),
    term:UObject({
        type:'term'
    }),
    cycle:UObject({
        type:'AttendanceCycle'
    }),
    periods:UObjectArray({
        type:'CheckTime'
    }),
    peopleCount:Integer()
});

let AttendanceCycle = um.model.createModel('AttendanceCycle',{
    cycleTime:Text(),
    cycleType:Text(),
    startPoint:Text()
});

let Festival = um.model.createModel('Festival', {
    name: Text(),
    start: DateTime(),
    end: DateTime(),
    status: Integer(),
    applicableRules:UObjectArray({
        type:'AttendanceRule'
    })
});

let MissedLesson = um.model.createModel('MissedLesson', {
    name: Text(),
    start: DateTime(),
    end: DateTime(),
    status: Integer(),
    applicableRules:UObjectArray({
        type:'AttendanceRule'
    })
});


let FestivalTip = um.model.createModel('FestivalTip', {
    name: Text(),
    start: DateTime(),
    end: DateTime(),
    status: Integer()
});

let Card = um.model.createModel('Card', {
    serialId: Text(),
    status: Integer()
});

let RawSignal = um.model.createModel('RawSignal', {
    type: Integer(),
    card: UObject({
        type: 'Card'
    }),
    classroom: UObject({
        type: 'Classroom'
    })
});

let RawAttendance = um.model.createModel('RawAttendance', {
    student: UObject({
        type: 'Student'
    }),
    type: Integer(),
    time: DateTime(),
    card: UObject({
        type: 'Card'
    }),
    classroom: UObject({
        type: 'Classroom'
    }),
    device: UObject({
        type: 'Device'
    })
});

let Attendance = um.model.createModel('Attendance', {
    student: UObject({
        type: 'Student'
    }),
    date: DateTime(),
    values: UObjectArray({
        type: 'AttendanceValue'
    })
});

let AttendanceValue = um.model.createModel('AttendanceValue', {
    enterStatus: Integer(),
    exitStatus: Integer()
});

let AbsenceApply = um.model.createModel('AbsenceApply', {
    student: UObject({
        type: 'Student'
    }),
    start: DateTime(),
    end: DateTime(),
    status: Integer(),
    comment: Text(),
    time: DateTime()
});

let Track = um.model.createModel('Track', {
    student: UObject({
        type: 'Student'
    }),
    time: DateTime(),
    classroom: UObject({
        type: 'Classroom'
    }),
    type: Integer()
});

let Teacher = um.model.createModel('Teacher', {
    tid: Text(),
    name: Text(),
    managedClasses: UObjectArray({
        type: 'NatureClass'
    }),
    managedSchool: UObject({
        type: 'School'
    }),
    managedGrade: UObject({
        type: 'Grade'
    }),
    user: UObject({
        type: 'User'
    }),
    labels:UObjectArray({
        type: 'Label'
    }),
    school:UObject({
        type: 'School'
    })
});

let AttendanceReportCache = um.model.createModel('AttendanceReportCache',{
    //periodType:Integer(),
    date:DateTime(),
    //endTime:DateTime(),
    school:UObject({
        type:'School'
    }),
    grade:UObject({
        type:'School'
    }),
    natureClass:UObject({
        type:'School'
    }),
    attendanceScope:Integer(),
    personTimeInfo:UObject({
        type:'AttendanceCachePersonTimeInfo'
    })

});

let AttendanceCachePersonTimeInfo = um.model.createModel('AttendanceCachePersonTimeInfo',{
    attendCount:Integer(),
    leaveCount:Integer(),
    lateCount:Integer(),
    absenceCount:Integer(),
    earlyCount:Integer()
});

let Label = um.model.createModel('Label',{
    name:Text(),
    checkTimeSet:UObject({
        type: 'CheckTimeSet'
    })
});

let Device = um.model.createModel('Device', {
    serialId: Text(),
    name: Text()
});

let Counter = um.model.createModel('Counter',{
    name: Text(),
    count: Integer()
});

let PeriodType = {
    date:2,
    week:1,
    month:0
};

let AttendanceScope = {
    school:0,
    grade:1,
    class:2
};

let FestivalStatus = {
    Detached: -1,
    Created: 0,
    Used: 1
};

let AttendanceStatus = {
    Created: 0,
    Attended: 1,
    AdditionalAttend: 2,
    Rest: 3,
    Absent: -1,
    Late: -2,
    Early: -3,
};

let RawAttendanceType = {
    EnterSchool: 0,
    ExitSchool: 1,
    EnterClassroom: 2,
    ExitClassroom: 3
};

let CheckTimeStatus = {
    Unused: 0,
    Used: 1
};

let TrackType = {
    EnterSchool: 0,
    ExitSchool: 1,
    EnterClassroom: 2, 
    ExitClassroom: 3, 
};

let AbsenceApplyStatus = {
    Processing: 0,
    Passed: 1,
    Rejected: 2
};

let CardStatus = {
    Enable: 1,
    Disable: 2,
    Loss: 3
};



let AttendanceDescriptions = new Map();
AttendanceDescriptions.set(0, '--');
AttendanceDescriptions.set(1, '出勤');
AttendanceDescriptions.set(2, '补出勤');
AttendanceDescriptions.set(3, '请假');
AttendanceDescriptions.set(-1, '缺勤');
AttendanceDescriptions.set(-2, '迟到');
AttendanceDescriptions.set(-3, '早退');

let UserDao = session.getDao(User);
let RoleDao = session.getDao(Role);
let PrivilegeDao = session.getDao(Privilege);
let CounterDao = session.getDao(Counter);
let SchoolDao = session.getDao(School);
let TermDao = session.getDao(Term);
let GradeDao = session.getDao(Grade);
let NatureClassDao = session.getDao(NatureClass);
let StudentDao = session.getDao(Student);
let ClassroomDao = session.getDao(Classroom);
let CheckTimeSetDao = session.getDao(CheckTimeSet);
let CheckTimeDao = session.getDao(CheckTime);
let FestivalDao = session.getDao(Festival);
let FestivalTipDao = session.getDao(FestivalTip);
let CardDao = session.getDao(Card);
let RawSignalDao = session.getDao(RawSignal);
let RawAttendanceDao = session.getDao(RawAttendance);
let AttendanceDao = session.getDao(Attendance);
let AttendanceValueDao = session.getDao(AttendanceValue);
let AbsenceApplyDao = session.getDao(AbsenceApply);
let TrackDao = session.getDao(Track);
let TeacherDao = session.getDao(Teacher);
let DeviceDao = session.getDao(Device);
let LabelDao = session.getDao(Label);
let AttendanceReportCacheDao = session.getDao(AttendanceReportCache);
let AttendanceCachePersonTimeInfoDao = session.getDao(AttendanceCachePersonTimeInfo);
let AttendanceRuleDao = session.getDao(AttendanceRule);
let AttendanceCycleDao = session.getDao(AttendanceCycle);
let MissedLessonDao = session.getDao(MissedLesson);


module.exports = {
    User,
    Role,
    Privilege,
    School,
    Grade,
    NatureClass,
    Student,
    Term,
    Classroom,
    CheckTimeSet,
    CheckTime,
    Festival,
    FestivalTip,
    Card,
    RawSignal,
    RawAttendance,
    Attendance,
    AttendanceValue,
    AbsenceApply,
    Track,
    Counter,
    Teacher,
    Device,
    Label,
    AttendanceReportCache,
    AttendanceCachePersonTimeInfo,
    AttendanceRule,
    AttendanceCycle,
    MissedLesson,
    UserDao,
    RoleDao,
    PrivilegeDao,
    CounterDao,
    SchoolDao,
    GradeDao,
    NatureClassDao,
    StudentDao,
    ClassroomDao,
    CheckTimeSetDao,
    CheckTimeDao,
    FestivalDao,
    FestivalTipDao,
    CardDao,
    RawSignalDao,
    RawAttendanceDao,
    AttendanceDao,
    AttendanceValueDao,
    AbsenceApplyDao,
    TrackDao,
    TermDao,
    TeacherDao,
    DeviceDao,
    LabelDao,
    AttendanceReportCacheDao,
    AttendanceCachePersonTimeInfoDao,
    FestivalStatus,
    AttendanceStatus,
    CheckTimeStatus,
    RawAttendanceType,
    TrackType,
    AbsenceApplyStatus,
    CardStatus,
    AttendanceDescriptions,
    PeriodType,
    AttendanceScope,
    AttendanceRuleDao,
    AttendanceCycleDao,
    MissedLessonDao
};
